package com.ruoyi.yl.rocketmq.consumer;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.yl.process.OrderProcess;
import com.ruoyi.common.domain.constant.enums.MessTopicAndTag;
import com.ruoyi.common.domain.constant.to.MessageActivityTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RocketMQMessageListener(
        topic = "yl_order",
        consumerGroup = "yl-consumer-order-group",
        selectorExpression = "*"  // 控制tag消费 在后面拼接  消费所有Tag的消息
)
public class OrderConsumer implements RocketMQListener<MessageExt> {

    private final OrderProcess orderProcess;

    public OrderConsumer(OrderProcess orderProcess) {
        this.orderProcess = orderProcess;
    }


    @Override
    public void onMessage(MessageExt messageExt) {
        MessageActivityTo to = JSON.parseObject(messageExt.getBody(), MessageActivityTo.class);

        if (MessTopicAndTag.YL_ORDER_VERIFICATION_PAY_STATUS.getTag().equals(messageExt.getTags())){
            try{
                //1.校验 只要不是未支付 就可以不用管啦
                if (orderProcess.verificationOrderPayStatus(to)){
                    //2. 校验到未支付  校验扣除信用积分 todo 这块还没写啊
                    log.info("DPY_ORDER_VERIFICATION_PAY_STATUS:{}","校验到未支付 扣除信用积分 暂时不写");
                }
            }catch (Exception e){
                log.error("消费消息失败:DPY_ORDER_VERIFICATION_PAY_STATUS，将自动重试", e);
                throw new RuntimeException("消费失败", e);
            }

        }

    }
}
